package com.loop.unclelend.ui.activity

import android.view.View
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiStepsBinding
import com.loop.unclelend.databinding.UncleItemStepTitleBinding
import com.loop.unclelend.pro.ProActivity
import com.loop.unclelend.ui.fragment.Fm_Step_Account
import com.loop.unclelend.ui.fragment.Fm_Step_Common
import com.loop.unclelend.ui.fragment.Fm_Step_Contact
import com.loop.unclelend.ui.fragment.Fm_Step_Ocr
import com.loop.unclelend.ui.fragment.Fm_Step_Selfie
import com.loop.unclelend.ui.presenter.Pre_Steps
import com.loop.unclelend.ui.view.IView_Steps
import com.loop.unclelend.utils.UnclePickContactUtils
import com.loop.unclelend.utils.event.UncleEventData
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/11 13:53
 * @Annotation：认证流程
 */
class Act_Steps: ProActivity(),IView_Steps {
    lateinit var binding: UiStepsBinding
    var presenter: Pre_Steps? = null

    //选择联系人
    var pickContact: UnclePickContactUtils? = null

    override fun getUncleRootView(): View {
        binding = UiStepsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarVisibility(View.VISIBLE)
        setUncleBackBtnRes(R.mipmap.icon_back)
        setUncleRightBtnRes(R.mipmap.icon_kefu)
        setUncleTextTitle(getString(R.string.steps_title))

        pickContact = UnclePickContactUtils(this)
    }

    override fun initUncleListener() {
        rootViewBinding.imgKefu.setOnClickListener {
            val eventRemark = presenter!!.getUncleCurrentStepTitle()
            showUncleKefuDialog(eventRemark)
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_CUSTOMER,eventRemark)
        }
        //流程认证完成
        binding.btnContinue.setOnClickListener {
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_STEP_COMPLETE_CONTINUER)
            onIntent(Act_Steps_Wait::class.java)
            finish()
        }
    }

    override fun initUncleData() {
        presenter = Pre_Steps(this)
        presenter!!.loadUncleProcessStepAction()
    }

    /**
     * 提交
     */
    fun onSubmitProcessAction(params: JSONObject){
        showUncleLoading()
        presenter!!.requestSubmitProcessAction(params)
    }

    /**
     * 移除所有View
     */
    override fun removeUncleStepAllViews() {
        binding.layoutAfter.removeAllViews()
        binding.layoutAfter.visibility = View.GONE
        binding.layoutCurrent.removeAllViews()
        binding.layoutBefore.removeAllViews()
        binding.layoutBefore.visibility = View.GONE
        binding.btnContinue.visibility = View.GONE
    }

    /**
     * 已完成的步骤
     */
    override fun addUncleStepAfterView(stepTitle: String) {
        val itemView = UncleItemStepTitleBinding.inflate(layoutInflater)
        itemView.txtStepTitle.text = stepTitle
        itemView.imgStepStatus.setImageResource(R.mipmap.icon_step_lable_complete)

        binding.layoutAfter.addView(itemView.root)
        binding.layoutAfter.visibility = View.VISIBLE
    }

    /**
     * 未开始的步骤
     */
    override fun addUncleStepBeforeView(stepTitle: String) {
        val itemView = UncleItemStepTitleBinding.inflate(layoutInflater)
        itemView.txtStepTitle.text = stepTitle
        itemView.imgStepStatus.setImageResource(R.mipmap.icon_step_lable_incomplete)

        binding.layoutBefore.addView(itemView.root)
        binding.layoutBefore.visibility = View.VISIBLE
    }

    /**
     * 所有步骤已完成
     */
    override fun showUncleStepsCompletePage() {
        binding.btnContinue.visibility = View.VISIBLE
    }

    /**
     * 通用样式
     */
    override fun showUncleStepCommonPage(stepItem: JSONObject) {
        val commonFm = Fm_Step_Common(stepItem)
        replaceContainerFragement(R.id.layout_current,commonFm)
    }

    /**
     * 联系人
     */
    override fun showUncleStepContactPage(stepItem: JSONObject) {
        val contactFm = Fm_Step_Contact(this,stepItem)
        replaceContainerFragement(R.id.layout_current,contactFm)
    }

    /**
     * Ocr
     */
    override fun showUncleStepOcrPage(stepItem: JSONObject) {
        val ocrFm = Fm_Step_Ocr(stepItem)
        replaceContainerFragement(R.id.layout_current,ocrFm)
    }

    /**
     * 自拍
     */
    override fun showUncleStepSelfiePage(stepItem: JSONObject) {
        //自拍照需要显示顶部提示
        val selfierFm = Fm_Step_Selfie(stepItem)
        replaceContainerFragement(R.id.layout_current,selfierFm)
    }

    /**
     * 法语地区添加联系人
     */
    override fun showUncleStepAccountPage(stepItem: JSONObject) {
        val accountFm = Fm_Step_Account(stepItem)
        replaceContainerFragement(R.id.layout_current,accountFm)
    }

    /**
     * 流程提交成功
     * 需要发送通知刷新总流程
     */
    override fun showUncleSubmitSuccess() {
        //通知刷新总流程
        publishEventData(UncleEventData(UncleEventData.TYPE.UNCLE_PROCESS_REFRESH))
    }

    override fun onUncleEvent(event: UncleEventData) {
        super.onUncleEvent(event)
        when(event.eventType){
            UncleEventData.TYPE.UNCLE_PROCESS_REFRESHED -> {
                presenter!!.loadUncleProcessStepAction()
            }
            else -> {

            }
        }
    }
}